package auctionhaus

import org.junit.*
import grails.test.mixin.*

@TestFor(BidController)
@Mock([Customer,Listing,Bid,BidCreateService])
class BidControllerTests {

    @Before
    void setUp()
    {
        //seed database with test data
        Customer seller = new Customer(email: "adambackstrom@gmail.com",password: "123456", enabled: true)
        assert seller.validate()
        seller.save(flush: true)

        Customer bidder = new Customer(email: "kleczka@gmail.com",password: "7891011", enabled: true)
        assert bidder.validate()
        bidder.save(flush: true)

        //The listing has a starting price of 1.0 and and end date 1 day in the future
        Listing listing = new Listing(name: "listing1",
                endDateTime: new Date() + 1,
                startingBidPrice: 10.0f,
                seller: seller)
        assert listing.validate()
        listing = listing.save(flush: true)

        println "Created Listing with id: " + listing.id
    }

    @Test
    //This includes test for HW3 UI-3
    void testAddBidOnListingValid()
    {
       assert Listing.count == 1
        
       //valid boostrapped data
       params.listing = 1
       //params.bidder = "kleczka@gmail.com"
       params.amount = 10000

       params.bidder = Customer.findByEmail("kleczka@gmail.com")
        
       controller.addBidOnListing()

       //validate we are sent to proper place
       //assert "/listing/show/1" == response.redirectedUrl

       //validate we populate success message
        assert controller.response.text == "Your bid has been accepted!"

       //validate bid is actually added
       assert Bid.count == 1
       Bid placedBid = Bid.findByListing(Listing.get(1))
       assert placedBid.bidder == Customer.findByEmail("kleczka@gmail.com")
       assert placedBid.amount == 10000
    }

    @Test
    void testAddBidOnListingInvalidListingId()
    {
        assert Listing.count == 1

        params.listing = 100
        params.bidder = "kleczka@gmail.com"
        params.amount = 10000

        controller.addBidOnListing()

        //validate we are sent to proper place
        assert view == "/listing/list"
        
        //validate we get the proper error message
        assert controller.flash.message != null
        assert controller.flash.message == "listing.not.found"

        //validate bid wasn't added
        assert Bid.count == 0
        
    }

    //@Test
    //THIS TEST IS NO LONGER VALID
    //THE BIDDER IS NOT PASSED IN VIA PARAMTER
    //BUT IS LOOKED UP VIA SPRING SECURITY LOGGED IN USER
    /*
    void testAddBidOnListingInvalidEmail()
    {
        assert Listing.count == 1

        params.listing = 1
        params.bidder = "1111111@gmail.com"
        params.amount = 10000

        controller.addBidOnListing()

        //validate we are redirected to proper place
        assert view == "/listing/show"

        //validate the bid was not added
        assert Listing.get(1).bids == null

        //validate the error message
        assert ((Bid)(model.bidInstance)).errors['bidder'].code == "nullable"
    }*/

    @Test
    //This includes test for HW3 UI-4
    void testAddBidOnListingInvalidBid()
    {
        assert Listing.count == 1

        params.listing = 1
        params.bidder = Customer.findByEmail("kleczka@gmail.com")
        params.amount = 1

        controller.addBidOnListing()

        //validate the bid was not added
        assert Listing.get(1).bids == null

        //validate we populate success message
        assert controller.response.text == "Your bid had errors and was unsuccessful!"
    }




}
